const router = require('koa-router')();
const {db, QueryTypes,} = require("../../models/db");
const tool = require("../../tools/tool");
// 保存客户
router.post('/contact/save', async ctx => {
    let user = ctx.user
    let tenantId = ctx.user.tenantId
    let body = ctx.request.body
    let formData = body.formData
    let id = formData.id
    let name = tool.xe.trim(formData.name)
    let pinYin = tool.py(name)
    let linkName = formData.linkName?tool.xe.trim(formData.linkName):''
    let linkMobile = formData.linkMobile?tool.xe.trim(formData.linkMobile):''
    let linkPhone = formData.linkPhone?tool.xe.trim(formData.linkPhone):''
    let address = formData.address?tool.xe.trim(formData.address):''
    let payType = formData.payType
    let categoryId = formData.categoryId?formData.categoryId:0
    let areaId = formData.areaId?formData.areaId:0
    let isCustomer = formData.isCustomer
    let isProvider = formData.isProvider
    let canLogin = formData.canLogin
    if(linkMobile) {
        if(!tool.checkPhone(linkMobile)) return ctx.err('手机号不正确')
        if(id) {
            let has = await db.query(`select count(*) total from (select linkMobile from contact where id<>${id} union all select username linkMobile from admin) a where linkMobile='${linkMobile}' `,
                {type: QueryTypes.SELECT, plain: true})
            if(has.total > 0) {
                return ctx.err('已存在相同手机号,手机号必须且唯一')
            }
        }
        else {
            let has = await db.query(`select count(*) total from (select linkMobile from contact union all select username linkMobile from admin) a where linkMobile='${linkMobile}' `,
                {type: QueryTypes.SELECT, plain: true})
            if(has.total > 0) {
                return ctx.err('已存在相同手机号,手机号必须且唯一')
            }
        }
    }
    let model;
    if(id) {
        model = await db.models['contact'].findOne({
            where: {
                tenantId,
                id
            }
        })
        if(!model) return ctx.err('未发现客户')
        model.uby = user.id
        model.utime = tool.getTime()
        if(!model.token) {
            model.token = tool.uuid()
        }
        if(!model.userpwd) {
            model.userpwd = tool.crypto.MD5('121121')
        }
    }
    else {
        model = db.models['contact'].build()
        model.tenantId = tenantId
        model.cby = user.id
        model.idDel = 0
        model.disabled = 0
        model.ctime = tool.getTime()
        model.token = tool.uuid()
        model.userpwd = tool.crypto.MD5('121121')

    }
    if(canLogin) {
        if(!linkMobile) {
            return ctx.err('允许登录必须设置正确的手机号')
        }
    }
    model.areaId = areaId
    model.categoryId = categoryId
    model.name = name
    model.linkName = linkName
    model.linkMobile = linkMobile
    model.linkPhone = linkPhone
    model.address = address
    model.payType = payType
    model.pinYin = pinYin
    model.isProvider = isProvider
    model.isCustomer = isCustomer
    model.canLogin = canLogin
    await model.save()
    if(formData.salesId && !id) {
        let sql = ` with bills as (select ${tenantId} tenantId,${model.id} buId, ${formData.salesId} salesId)
        merge into contact_sales a using bills b on a.tenantId=b.tenantId and a.buId=b.buId and a.salesId=b.salesId 
        when not matched then insert (tenantId,buId,salesId) values (b.tenantId,b.buId,b.salesId);`
        await db.query(sql, {type: QueryTypes.UPDATE})
    }
    if(id) {
        return  ctx.suc('修改成功')
    }
    else {
        return ctx.suc('新增成功')
    }
})




module.exports = router;